var totalSubDivisionInPerches = 0.0;

$(function() {
    var minHe = 0,minAc = 0,minRo = 0,minPe = 0,minTemp = 0;
    var minCont = 2.47105381467;
    $('#minHectareId').change(function() {
        changeMinHectare();
        calculateTotalSubDivisionInPerches();
        $('#minimumSubDivisionUnit').val(totalSubDivisionInPerches);
    });
    $('#minAcreId').change(function() {
        minAc = $("#minAcreId").val();
        minTemp = parseFloat(minAc % 1);
        if (minTemp >= 0) {
            $("#minAcreId").val(Math.floor(minAc))
            minRo = $("#minRoodsId").val();
            if (!$("#minRoodsId").val().length) {
                minRo = 0;
            }
            minTemp = parseFloat(minRo) + (minTemp * 4);
            var minTemp2 = parseFloat(minTemp % 1);

            $("#minRoodsId").val(Math.floor(minTemp));
            if (minTemp2 >= 0) {
                minPe = $("#minPerchesId").val();
                if (!$("#minPerchesId").val().length) {
                    minPe = 0;
                }
                minTemp = parseFloat(minPe) + (minTemp2) * 40;
                $("#minPerchesId").val(minTemp.toFixed(4));
            }

        }
        setMinHect();
        calculateTotalSubDivisionInPerches();
        $('#minimumSubDivisionUnit').val(totalSubDivisionInPerches);
    });
    $('#minRoodsId').change(function() {
        minRo = $("#minRoodsId").val();
        if (!$("#minRoodsId").val().length) {
            minRo = 0;
        }
        minTemp = parseFloat(minRo % 1);
        if (minTemp >= 0) {
            $("#minRoodsId").val(Math.floor(minRo));
            minPe = $("#minPerchesId").val();
            if (!$("#minPerchesId").val().length) {
                minPe = 0;
            }
            minTemp = parseFloat(minPe) + (minTemp) * 40;
            $("#minPerchesId").val(minTemp.toFixed(4));
        }
        checkMinRoodsLimit();
        setMinHect();
        calculateTotalSubDivisionInPerches();
        $('#minimumSubDivisionUnit').val(totalSubDivisionInPerches);
    });
    $('#minPerchesId').change(function() {
        checkMinPerchesLimit();
        setMinHect();
        calculateTotalSubDivisionInPerches();
        $('#minimumSubDivisionUnit').val(totalSubDivisionInPerches);
    });
});

function checkMinRoodsLimit() {
    var minHe = 0,minAc = 0,minRo = 0,minPe = 0,minTemp = 0;
    minRo = $("#minRoodsId").val();
    if (!$('#minRoodsId').val().length) {
        minRo = 0;
    }
    minRo = parseFloat(minRo);
    minTemp = minRo;
    minRo = minRo % 4;
    $("#minRoodsId").val(minRo);
    minAc = $("#minAcreId").val();
    if (!$('#minAcreId').val().length) {
        minAc = 0;
    }
    minAc = parseFloat(minAc)
    minAc = minAc + (minTemp - minRo) / 4;
    $('#minAcreId').val(minAc);
}
function checkMinPerchesLimit() {
    var minHe = 0,minAc = 0,minRo = 0,minPe = 0,minTemp = 0;
    minPe = $('#minPerchesId').val();
    if (!$('#minPerchesId').val().length) {
        minPe = 0;
    }
    minTemp = minPe;
    minPe = parseFloat(minPe);
    minPe = minPe % 40;
    $("#minPerchesId").val(minPe);
    minRo = $("#minRoodsId").val();
    if (!$('#minRoodsId').val().length) {
        minRo = 0;
    }
    minRo = parseFloat(minRo);
    minRo = minRo + (minTemp - minPe) / 40;
    $("#minRoodsId").val(minRo);
    checkMinRoodsLimit();
}
function setMinHect() {
    var minHe = 0,minAc = 0,minRo = 0,minPe = 0,minTemp = 0;
    var minCont = 2.47105381467;
    minAc = $("#minAcreId").val();
    minRo = $("#minRoodsId").val();
    minPe = $("#minPerchesId").val();
    if (!$("#minAcreId").val().length) {
        minAc = 0;
    }
    if (!$("#minRoodsId").val().length) {
        minRo = 0;
    }
    if (!$("#minPerchesId").val().length) {
        minPe = 0;
    }
    minAc = parseFloat(minAc);
    minRo = parseFloat(minRo);
    minPe = parseFloat(minPe);

    minRo = parseFloat(minRo + minPe / 40);
    minAc = parseFloat(minAc + minRo / 4);
    minHe = parseFloat(minAc / minCont).toFixed(4);
    $("#minHectareId").val(minHe);
}
function changeMinHectare() {
    var minHe = 0,minAc = 0,minRo = 0,minPe = 0,minTemp = 0;
    var minCont = 2.47105381467;
    minHe = ($('#minHectareId').val().length == 0) ? 0 : $('#minHectareId').val();
    minAc = Math.floor(minHe * minCont);
    minTemp = (minHe * minCont - minAc) * 4;
    minRo = Math.floor(minTemp);
    minTemp = (minTemp - minRo) * 40;
    minPe = minTemp.toFixed(4);
    $("#minAcreId").val(minAc);
    $("#minRoodsId").val(minRo);
    $("#minPerchesId").val(minPe);
}

function calculateTotalSubDivisionInPerches() {
    totalSubDivisionInPerches = (parseFloat($('#minAcreId').val()) * 160) + (parseFloat($('#minRoodsId').val()) * 40) + parseFloat($('#minPerchesId').val());
}

$(function() {
    if (parseFloat($('#minimumSubDivisionUnit').val()) > 0) {
        fillSubDivision();
    }
});

function fillSubDivision() {
    totalSubDivisionInPerches = $('#minimumSubDivisionUnit').val();
    if (!totalSubDivisionInPerches.length) {
        totalSubDivisionInPerches = 0.0;
    }
    totalSubDivisionInPerches = parseFloat(totalSubDivisionInPerches);
    var minPe = totalSubDivisionInPerches % 40;
    var minRo = ((totalSubDivisionInPerches - minPe) / 40) % 4;
    var minAc = ((((totalSubDivisionInPerches - minPe) / 40) - minRo) / 4);
    var minHe = totalSubDivisionInPerches * 0.002529285264;
    $("#minAcreId").val(minAc);
    $("#minRoodsId").val(minRo);
    $("#minPerchesId").val(minPe.toFixed(4));
    $("#minHectareId").val(minHe.toFixed(4));
}